//
//  DBStatusesCell.swift
//  大波微博swift
//
//  Created by 大波 on 16/3/4.
//  Copyright © 2016年 zhaojin. All rights reserved.
//

import UIKit
import SnapKit

class DBStatusesCell: UITableViewCell {
    
    private let DBStatusesCellMargin = 8
    
    private var bottomConstrain: Constraint?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var statuse: DBStatuses? {
        
        didSet {
            
            originalView.statuse = statuse
            
            bottomConstrain?.uninstall()
            
            if let retweeted = statuse?.retweeted_status {
                
                retweetedView.hidden = false
                
                retweetedView.retweetedStatuse = retweeted
                
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstrain = make.top.equalTo(retweetedView.snp_bottom).offset(DBStatusesCellMargin).constraint
                })
                
            } else {
                
                retweetedView.hidden = true
                
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.bottomConstrain = make.top.equalTo(originalView.snp_bottom).offset(DBStatusesCellMargin).constraint
                })
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        
        prepareUI()
    }
    
    func prepareUI() {
        
        contentView.addSubview(originalView)
        
        contentView.addSubview(bottomView)
        
        contentView.addSubview(retweetedView)
        
        originalView.snp_makeConstraints { (make) -> Void in
            
//            make.leading.equalTo(contentView).offset(DBStatusesCellMargin - 1)
//
//            make.trailing.equalTo(contentView).offset(-DBStatusesCellMargin + 1)
//            
//            make.top.equalTo(contentView).offset(DBStatusesCellMargin)
            
            
            make.leading.trailing.equalTo(contentView)
            make.top.equalTo(contentView).offset(10)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            
//            make.top.equalTo(originalView.snp_bottom).offset(DBStatusesCellMargin)
//            
//            make.leading.trailing.equalTo(contentView)
//            
//            make.height.equalTo(44)
            
            
            make.leading.trailing.equalTo(contentView)
            make.height.equalTo(44)
            
            self.bottomConstrain = make.top.equalTo(retweetedView.snp_bottom).constraint
        }
        
        retweetedView.snp_makeConstraints { (make) -> Void in
            
//            make.top.equalTo(originalView.snp_bottom).offset(DBStatusesCellMargin)
//            
//            make.leading.equalTo(contentView).offset(DBStatusesCellMargin)
//            
//            make.trailing.equalTo(contentView).offset(-DBStatusesCellMargin)
            
            
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        contentView.snp_makeConstraints { (make) -> Void in
            
//            make.leading.top.trailing.equalTo(self)
//            
//            make.bottom.equalTo(bottomView)
            
            
            make.leading.trailing.top.equalTo(self)
            
            // contentView的底部
            make.bottom.equalTo(bottomView)
        }
    }
    
    // MARK: - 懒加载
    
    /// 原创View
    private lazy var originalView: DBOriginalView = DBOriginalView()
    
    /// 底部View
    private lazy var bottomView: DBBottomView = DBBottomView()
    
    /// 转发View
    private lazy var retweetedView: DBRetweetedView = DBRetweetedView()
}


























